home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8245 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.3 KB

  1. Path: erich.triumf.ca!bennett
  2. From: bennett@erich.triumf.ca (P.Bennett)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Checking for Ilegal Input
  5. Date: 2 Mar 1996 09:05 PST
  6. Organization: TRIUMF: Tri-University Meson Facility
  7. Distribution: world
  8. Message-ID: <2MAR199609054277@erich.triumf.ca>
  9. References: <313855C7.5E86@aol.com>
  10. NNTP-Posting-Host: erich.triumf.ca
  11. News-Software: VAX/VMS VNEWS 1.50    
  12.  
  13. In article <313855C7.5E86@aol.com>, gregace@aol.com writes...
  14. >I have a program where you must enter a number between 0 and 15 and check for 
  15. >illegal input such as a number not between 0 and 15 or if a character is 
  16. >entered.
  17. >I used the scanf("%d",&number) to read the input.
  18.  
  19. scanf() should be banned!
  20.  
  21. It is very intolerant of unexpected input, as you have found.  Instead of
  22. reading the user's input directly with scanf(), get the input with fgets(),
  23. then you can use sscanf(), and check it's return value to see if it got a
  24. number.
  25.  
  26. Peter Bennett VE7CEI                | Vessels shall be deemed to be in sight
  27. Internet: bennett@triumf.ca         | of one another only when one can be
  28. Packet: ve7cei@ve7kit.#vanc.bc.ca   | observed visually from the other
  29. TRIUMF, Vancouver, B.C., Canada     |                          ColRegs 3(k)
  30. GPS and NMEA info and programs: ftp://sundae.triumf.ca/pub/peter/index.html
  31.  
  32.  
  33.  
  34.  
  35.  
  36.